package com.lovo.boot.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

/**
 * @title 模型转换工具类
 * @author: luo
 * @date: 2020/11/24 22:05
 */
@Slf4j
public final class PojoTransferUtil {
    /**
     * 复制属性
     *
     * @param srcObj      源对象
     * @param targetClass 目标模板
     * @return 得到对应属性的对象
     */
    public static <Target> Target transfer(Object srcObj, Class<Target> targetClass) {

        if (srcObj == null || targetClass == null) {
            return null;
        }

        try {

            Target newInstance = targetClass.newInstance();
            BeanUtils.copyProperties(srcObj, newInstance);
            return newInstance;

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        return null;
    }

}
